package leetcode.code0689;

import leetcode.helper.HelpDebug;
import leetcode.helper.MaxDebug;

public class Solution extends S689 {

	@Override
	public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
		int Len = nums.length;
		int[] sum = new int[Len + 1];
		for (int i = 1; i <= Len; i++) {
			sum[i] = nums[i - 1] + sum[i - 1];
		}
		int[][] dp = new int[4][Len + 1];
		for (int i = 1; i <= 3; i++) {
			for (int j = k; j <= Len; j++) {
				dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j - k] + sum[j] - sum[j - k]);
			}
		}
		HelpDebug.print(dp);
		int[] ans = new int[3];
		for (int i = Len, p = 2, no = 3, find = dp[3][Len]; find != 0 && i >= 0 && p >= 0; i--) {
			if (dp[no][i] != find) {
				i = i + 1 - k;
				ans[p--] = i;
				find = dp[--no][i];
			}
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		MaxDebug.debug14(so);
	}

}
